package com.fh.fruit.controller.plantbase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.entity.plantbase.PlantBase;
import com.fh.fruit.service.basearea.BaseAreaManager;
import com.fh.fruit.service.plantbase.PlantBaseManager;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/plantbase")
public class PlantBaseController extends BaseController {
	String menuUrl = "plantbase/list.do"; // 种植基地列表(权限用)
	@Resource(name = "plantBaseService")
	private PlantBaseManager plantBaseService;
	@Resource(name="baseAreaService")
	private BaseAreaManager baseAreaService;

	/**
	 * 显示基地列表
	 * 
	 * @param page
	 * @param httpSession
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public ModelAndView plantBasesList(Page page, HttpSession httpSession) throws Exception {
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String name = pd.getString("name"); // 关键词检索条件
		if (StringUtils.isNotEmpty(name)) {
			pd.put("name", name.trim());
		}
		page.setPd(pd);
		List<PlantBase> plantBasesList = plantBaseService.listPlantBases(page); // 列出基地列表
		mv.setViewName("farm/plantbase/plantBase_list");
		mv.addObject("plantBasesList", plantBasesList);
		mv.addObject("pd", pd);
		mv.addObject("QX", Jurisdiction.getHC()); // 按钮权限
		return mv;
	}

	/**
	 * 去新增种植基地
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAddP")
	public ModelAndView goAddP(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		} // 校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String state = pd.getString("state");
		mv.setViewName("farm/plantbase/plantBase_edit");
		mv.addObject("msg", "saveP");
		mv.addObject("state", state);
		mv.addObject("pd", pd);
		return mv;
	}

	/**
	 * 去修改基地信息
	 * 
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goEditP")
	public ModelAndView goEditP(Page page) throws Exception {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		}
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String state = pd.getString("state");
		PlantBase plantbase = plantBaseService.findById(pd);
		mv.setViewName("farm/plantbase/plantBase_edit");
		mv.addObject("pd", pd);
		mv.addObject("state", state);
		mv.addObject("msg", "editP");
		mv.addObject("plantbase", plantbase);
		return mv;
	}
	
	/**
	 * 编辑保存基地信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editP")
	public ModelAndView editP() throws Exception {
		// 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "修改种植基地信息");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {
			return null;
		}
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try {
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			pd.put("operatorId", user.getUSER_ID()); // 修改人
			pd.put("updateTime", DateUtil.getTime()); // 修改时间
			int rec=plantBaseService.updatePlantBases(pd);
			if(rec<=0){
				mv.addObject("msg", "失败，操作数据不存在！");
			}
		} catch (Exception e) {
			mv.addObject("msg", "failed");
			logger.error(e.toString(), e);
		}
		logAfter(logger);
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 新增保存基地信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveP")
	public ModelAndView saveP() throws Exception {
		// 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "新增种植基地信息");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {
			return null;
		}
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try {
			User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
			pd.put("operatorId", user.getUSER_ID()); // 操作人
			pd.put("createTime", DateUtil.getTime()); // 创建时间
			plantBaseService.savePlantBases(pd);
		} catch (Exception e) {
			mv.addObject("msg", "failed");
			logger.error(e.toString(), e);
		}
		logAfter(logger);
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 删除基地
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteP")
	@ResponseBody
	public String deleteP(HttpServletRequest requset) {
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
			return null;
		} // 校验权限
		logBefore(logger, Jurisdiction.getUsername() + "删除种植基地");
		PageData pd = this.getPageData();
		String baseId = pd.getString("ids"); // 基地id
		if (StringUtils.isNotEmpty(baseId)) {
			pd.put("baseId", baseId.trim());
		}else{
			//03：基地ID不能为空
			return "03";
		}
		try {
			PageData prmas=new PageData();
			prmas.put("baseId", baseId.trim());
			int result=baseAreaService.selectTotalBaseArea(prmas);
			//基地下有区域时不能删除基地
			if(result>0){
				//02：基地下存在区域
				return "02";
			}
			int rec=plantBaseService.deletePlantBases(pd);
			if(rec>0){
				//01：删除成功
				return "01";
			}else{
				//00：删除失败
				return "00";
			}			
		} catch (Exception e) {
			logger.error(e.toString(), e);
			return "failed";
		}
	}

	/**
	 * 判断基地名称或者编码是否存在
	 * 
	 * @return
	 */
	@RequestMapping(value = "/hasU")
	@ResponseBody
	public Object hasU() {
		String result = "success";
		PageData pd = this.getPageData();
		try {
			List<PageData> list=plantBaseService.findByNameOrCode(pd);
			if (list!= null&&list.size()>0) {
				result = "error";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", result);
		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 判断基地者编码是否存在
	 * 
	 * @return
	 */
	@RequestMapping(value = "/hasN")
	@ResponseBody
	public Object hasN() {
		String result = "success";
		PageData pd = this.getPageData();
		try {
			List<PageData> list=plantBaseService.findByNameOrCode(pd);
			if (list!= null&&list.size()>0) {
				result = "error";
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", result);
		return AppUtil.returnObject(new PageData(), map);
	}
	/**
	 * 地区下拉信息
	 */
	@ResponseBody
	@RequestMapping(value = "realList")
	public Map<String, Object> list(Integer parentId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd.put("parentId", parentId);
		pd = this.getPageData();
		try {
			List<PageData> list = plantBaseService.list(pd);
			map.put("list", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
}
